package top.xbaoziplus.company.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xbaoziplus.company.empty.Appointment;
import top.xbaoziplus.company.service.AppointmentService;
import top.xbaoziplus.company.vo.PositionVo;
import top.xbaoziplus.company.common.Result;
import top.xbaoziplus.company.empty.Position;
import top.xbaoziplus.company.service.PositionService;

import java.util.List;

/**
 * @author xBaozi
 * @version 1.0
 * @classname PositionController
 * @description 职位控制器
 * @date 2022/6/29 9:24
 */
@RestController
@RequestMapping("/position")
@Slf4j
@Api(tags = "职位模块")
public class PositionController {
    @Autowired
    PositionService positionService;

    @Autowired
    private AppointmentService appointmentService;

    @ApiOperation("添加职位")
    @PostMapping("/add")
    public Result addPosition(@RequestBody @ApiParam("传name和description过来") Position position){
        return positionService.add(position);
    }

    @ApiOperation("逻辑删除职位")
    @DeleteMapping("/delete")
    public Result deletePosition(@RequestParam @ApiParam("传职位ID") Integer id){
        if (!ObjectUtils.isNull(id)){
            LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Position::getId, id);
            Position position = new Position();
            position.setStatus(1);
            boolean b = positionService.update(position, queryWrapper);
            if (b){
                return Result.success("移除成功");
            }
        }
        return Result.fail("移除失败");
    }

    @ApiOperation("恢复职位")
    @PostMapping("recovery")
    public Result recoveryPosition(@RequestParam @ApiParam("传ID") Integer id){
        return positionService.recovery(id);
    }

    @ApiOperation("获取职位列表（下拉框）")
    @PostMapping("list")
    public Result listPosition(){
        List<Position> list = positionService.list();
        return Result.success(list, "获取职位列表成功");
    }

    @ApiOperation("修改职位信息")
    @PostMapping("modify")
    public Result modify(@RequestBody @ApiParam("传职位对象") Position position){

        LambdaQueryWrapper<Appointment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Appointment::getPositionId, position.getId());
        Appointment appointment = new Appointment();
        appointment.setPositionName(position.getName());
        appointmentService.update(appointment, queryWrapper);

        boolean b = positionService.updateById(position);
        if (b){
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    @ApiOperation("分页展示职位信息")
    @PostMapping("listPage")
    public Result listPage(@RequestBody @ApiParam("传职位名称、当前页码、一页显示总数量过来") PositionVo positionVo){
        return positionService.listPage(positionVo);
    }

    @ApiOperation("分页展示已经删除职位信息")
    @PostMapping("listDeletedPage")
    public Result listDeletedPage(@RequestBody @ApiParam("传职位名称、当前页码、一页显示总数量过来") PositionVo positionVo){
        return positionService.listDeletedPage(positionVo);
    }

}
